package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.news.AddNewsModel;
import com.dcy.znoa.controller.publiz.model.news.LogicDelNewsModel;
import com.dcy.znoa.controller.publiz.model.news.SelectNewsModel;
import com.dcy.znoa.controller.publiz.model.news.UpdateNewsModel;
import com.dcy.znoa.domain.publiz.News;
import com.dcy.znoa.service.publiz.NewsService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author pengyu
 */
//新闻 controller ，这个的insert是对外提供服务，需要登录
@Controller
@RequestMapping("news")
public class NewsController extends BaseController {
    @Autowired
    NewsService newsService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectNewsModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return newsService.selectList(model);
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddNewsModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        News cp = new News();
        BeanUtils.copyProperties(cp, model);
        return newsService.insert(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateNewsModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        News cp = new News();
        BeanUtils.copyProperties(cp, model);
        return newsService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelNewsModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        News cp = new News();
        BeanUtils.copyProperties(cp, model);
        return newsService.updateById(cp);
    }

}
